﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Text;
using xbd.DataConvertLib;

namespace TaoYL.BoosterStation
{
    public class HistoryDataService : Singleton<HistoryDataService>
    {
        private HistoryDataService() { }

        public bool AddHistoryData(HistoryData data)
        {
            try
            {
                string sql = "insert into plchistorydata(InsertTime,TempIn1,TempIn2,TempOut,PressureTank1,PressureTank2,LevelTank1,LevelTank2,PressureTankOut) " +
    "values(@InsertTime,@TempIn1,@TempIn2,@TempOut,@PressureTank1,@PressureTank2,@LevelTank1,@LevelTank2,@PressureTankOut)";

                SQLiteParameter[] SqlParameter = new SQLiteParameter[]
                {
                     new SQLiteParameter("@InsertTime",data.InsertTime.ToString("yyyy-MM-dd HH:mm:ss")),
                    new SQLiteParameter("@TempIn1",data.TempIn1),
                    new SQLiteParameter("@TempIn2",data.TempIn2),
                    new SQLiteParameter("@TempOut",data.TempOut),
                    new SQLiteParameter("@PressureTank1",data.PressureTank1),
                    new SQLiteParameter("@PressureTank2",data.PressureTank2),
                    new SQLiteParameter("@LevelTank1",data.LevelTank1),
                    new SQLiteParameter("@LevelTank2",data.LevelTank2),
                    new SQLiteParameter("@PressureTankOut",data.PressureTankOut)
                };

                int result = SQLiteHelper.ExecuteNonQuery(sql, SqlParameter);

                return result > 0;
            }
            catch (Exception ex)
            {
                //log.Error("保存历史数据失败", ex);
                return false;
            }
        }


        public OperateResult<List<HistoryData>> GetHistoryDataByTime(DateTime start, DateTime end)
        {

            string sql = "select * from historydata where InsertTime between @start and @end";

            SQLiteParameter[] SqlParameter = new SQLiteParameter[]
            {
                new SQLiteParameter("@start", start.ToString("yyyy-MM-dd HH:mm:ss")),
                new SQLiteParameter("@end", end.ToString("yyyy-MM-dd HH:mm:ss"))
            };

            try
            {
                SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql, SqlParameter);
                List<HistoryData> historyDatas = new List<HistoryData>();
                while (reader.Read())
                {
                    HistoryData data = new HistoryData();
                    data.InsertTime = DateTime.Parse(reader["InsertTime"].ToString());
                    data.TempIn1 = float.Parse(reader["TempIn1"].ToString());
                    data.TempIn2 = float.Parse(reader["TempIn2"].ToString());
                    data.TempOut = float.Parse(reader["TempOut"].ToString());
                    data.PressureTank1 = float.Parse(reader["PressureTank1"].ToString());
                    data.PressureTank2 = float.Parse(reader["PressureTank2"].ToString());
                    data.LevelTank1 = float.Parse(reader["LevelTank1"].ToString());
                    data.LevelTank2 = float.Parse(reader["LevelTank2"].ToString());
                    data.PressureTankOut = float.Parse(reader["PressureTankOut"].ToString());
                    historyDatas.Add(data);

                    // 这里可以使用反射来设置属性值
                    //var historyData = new HistoryData();
                    //for (int i = 0; i < reader.FieldCount; i++)
                    //{
                    //    var propertyName = reader.GetName(i);
                    //    var property = typeof(HistoryData).GetProperty(propertyName);
                    //    if (property != null)
                    //    {
                    //        property.SetValue(historyData, reader.GetValue(i));
                    //    }
                    //}
                }
                reader.Close();
                return OperateResult.CreateSuccessResult(historyDatas);
            }
            catch (Exception ex)
            {
                return OperateResult.CreateFailResult<List<HistoryData>>(ex.Message);
            }

        }

        /// <summary>
        /// 单个区间查询报表方法
        /// </summary>
        /// <param name="start">起始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="condition">查询内容</param>
        /// <param name="datatableName">数据表名</param>
        /// <returns>带操作结果的DataTable</returns>
        public OperateResult<DataTable> GetReportDataByCondition(string start, string end,List<string> condition,string datatableName)
        {
            StringBuilder stringBuilder =new StringBuilder();
            stringBuilder.Append("select ");
            stringBuilder.Append(string.Join(",", condition));
            stringBuilder.Append(" from HistoryData where InsertTime between @Start and @End");

            SQLiteParameter[] SqlParameter = new SQLiteParameter[]
            {
                new SQLiteParameter("@start", start),
                new SQLiteParameter("@end", end)
            };

            
            try
            {
                DataSet dataSet = SQLiteHelper.GetDataSet(stringBuilder.ToString(), SqlParameter,datatableName);

                if (dataSet.Tables.Count>0)
                {
                    return OperateResult.CreateSuccessResult(dataSet.Tables[0]);
                }
                else
                {
                    return OperateResult.CreateFailResult<DataTable>("没有查询到数据");
                }
            }
            catch (Exception ex)
            {
                return OperateResult.CreateFailResult<DataTable>(ex.Message);
            }
        }
    }
}

